//
//  ViewController.m
//  BShop
//
//  Created by chenguibang on 2017/8/8.
//  Copyright © 2017年 chenguibang. All rights reserved.
//

#import "ViewController.h"
#import <AliyunOSSiOS/OSSService.h>
#import <AFNetworking/AFNetworking.h>
#import "YTLoginController.h"
#import <SDImageCache.h>
@interface ViewController (){
    OSSClient * client;
}

@end


NSString * const endPoint = @"https://oss-cn-shanghai.aliyuncs.com";
@implementation ViewController


- (instancetype)initWithCoder:(NSCoder *)aDecoder{
   self = [super initWithCoder:aDecoder];
    if (self) {
        [self setupItem];
    }
    return self;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        
    }
    return self;
}


- (void)setupItem{
    self.userCenterController = [[UserCenterController alloc]init];
    self.userCenterController.title = @"用户中心";
    


    
    UINavigationController *userCenterNavController = [[UINavigationController alloc]initWithRootViewController:self.userCenterController];
    userCenterNavController.tabBarItem.title = @"个人中心";
    userCenterNavController.tabBarItem.image = [UIImage imageNamed:@"user_tab"];
    
    self.viewControllers = @[userCenterNavController];
    self.tabBar.tintColor = [APPManager shared].theme.navigationBarBackgorundColor;
}




- (void)showOn{
    [self presentViewController:[[UINavigationController alloc] initWithRootViewController:[YTLoginController new]] animated:NO completion:^{
        
    }];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
  
    
    [self initLocalFile];
    
//    Access Key Id: STS.MALgwugkgVjM3iB23TJMRQu4v
//    Access Key Secret: 2sUWjAe5qMdW2qoZUR7VnwWyhd3e2ETNSpMoysqpkfSB
//    Security Token: CAISiwJ1q6Ft5B2yfSjIrIL5LM3BirRG4aimMU/z1jMBRsJ+vrCflDz2IHlNdHdtBegWsfs/mWtR7PYflqB6T55OSAmcNZIoBCSFI7fiMeT7oMWQweEuuv/MQBquaXPS2MvVfJ+OLrf0ceusbFbpjzJ6xaCAGxypQ12iN+/m6/Ngdc9FHHP7D1x8CcxROxFppeIDKHLVLozNCBPxhXfKB0ca3WgZgGhku6Ok2Z/euFiMzn+Ck75O9tShecD0NpEwZcYhDu3YhrImKvDztwdL8AVP+atMi6hJxCzKpNn1ASMKukzWbbeIqo0wc18kPfFgS/Ne1/Lnlfpg//HImp6y0RtEMeZTVzXPgXgGZyAmyYgagAEbjmuvfMJi41Bpa2JJ2LRUl/dC1Y14YJqQmoTP+sRzAyiVzwGOY1xbfJE5EzwKV7/nzQLzQ290LiLtVHocN9WYt6F8xBLp7hGN9jaxbtEIUA/k8aNf9YGkVUCFr9sqJYw2r64XTKa105yuKEQ7A9PUceUSPKVpyV/2d/8K3MCdPg==

    

    
    id<OSSCredentialProvider> credential = [[OSSStsTokenCredentialProvider alloc]initWithAccessKeyId:@"STS.FPsGnY39nrZcuTaGxf2n7Zf8o" secretKeyId:@"D9DcsW6msZn3VCtzTmS3LP9NssdM18W4SRc274SqB7Wc" securityToken:@"CAISiwJ1q6Ft5B2yfSjIp5PGDNTt3uZPxZiId3LQo3gzPuEbtaOTjTz2IHlNdHdtBegWsfs/mWtR7PYflqB6T55OSAmcNZIoNjLFSLfiMeT7oMWQweEuuv/MQBquaXPS2MvVfJ+OLrf0ceusbFbpjzJ6xaCAGxypQ12iN+/m6/Ngdc9FHHP7D1x8CcxROxFppeIDKHLVLozNCBPxhXfKB0ca3WgZgGhku6Ok2Z/euFiMzn+Ck75O9tShecD0NpEwZcYhDu3YhrImKvDztwdL8AVP+atMi6hJxCzKpNn1ASMKukzWbbeIqo0wc18kPfFgS/Ne1/Lnlfpg//HImp6y0RtEMeZTVzXPgXgGZyAmyYgagAEoCCTJKn26Vq4evV+uIJlbGQfX7T0TiIj4ZhBlm4BJ67n9bvSg+bh5oIfrzUuxwIZ2psU4gzpd3lBIEYoX2tdpC6hhq4/iSdRq/Za5hQZgUAvgtShhMaz69kweR9skHPCMp0Jbo2r4oWDlZ1BpIWrODodJ7gwGCxC8Pe+BDRvFww=="];
    client = [[OSSClient alloc] initWithEndpoint:endPoint credentialProvider:credential];
}


// create some random file for demo cases
- (void)initLocalFile {
    NSFileManager * fm = [NSFileManager defaultManager];
    NSString * mainDir = [self getDocumentDirectory];
    
    NSArray * fileNameArray = @[@"file1k", @"file10k", @"file100k", @"file1m", @"file10m", @"fileDirA/", @"fileDirB/"];
    NSArray * fileSizeArray = @[@1024, @10240, @102400, @1024000, @10240000, @1024, @1024];
    
    NSMutableData * basePart = [NSMutableData dataWithCapacity:1024];
    for (int i = 0; i < 1024/4; i++) {
        u_int32_t randomBit = arc4random();
        [basePart appendBytes:(void*)&randomBit length:4];
    }
    
    for (int i = 0; i < [fileNameArray count]; i++) {
        NSString * name = [fileNameArray objectAtIndex:i];
        long size = [[fileSizeArray objectAtIndex:i] longValue];
        NSString * newFilePath = [mainDir stringByAppendingPathComponent:name];
        if ([fm fileExistsAtPath:newFilePath]) {
            [fm removeItemAtPath:newFilePath error:nil];
        }
        [fm createFileAtPath:newFilePath contents:nil attributes:nil];
        NSFileHandle * f = [NSFileHandle fileHandleForWritingAtPath:newFilePath];
        for (int k = 0; k < size/1024; k++) {
            [f writeData:basePart];
        }
        [f closeFile];
    }
    NSLog(@"main bundle: %@", mainDir);
}


- (NSString *)dateTransformToTimeString
{
    NSDate *currentDate = [NSDate date];//获得当前时间为UTC时间 2014-07-16 07:54:36 UTC  (UTC时间比标准时间差8小时)
    //转为字符串
    NSDateFormatter*df = [[NSDateFormatter alloc]init];//实例化时间格式类
    [df setDateFormat:@"YYYY-MM-DD hh:mm:ssZ"];//格式化
    //2014-07-16 07:54:36(NSString类)
    NSString *timeString = [df stringFromDate:currentDate];
    return timeString;
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
   
    
    [self presentViewController:[[UINavigationController alloc] initWithRootViewController:[YTLoginController new]] animated:YES completion:^{
        
    }];

    
    
   
    
    
    

    OSSPutObjectRequest * put = [OSSPutObjectRequest new];
    
    // required fields
    put.bucketName = @"bshop-user";
    put.objectKey = @"123546875654/file1m";
    NSString * docDir = [self getDocumentDirectory];
    put.uploadingFileURL = [NSURL fileURLWithPath:@"/var/mobile/Media/DCIM/101APPLE/IMG_1266.JPG"];
//    file:///var/mobile/Media/DCIM/101APPLE/IMG_1266.JPG
//    file:///var/mobile/Containers/Data/Application/5C4943AF-E6EB-4320-B44C-A70BA9BE86BB/Documents/file1m
    put.uploadProgress = ^(int64_t bytesSent, int64_t totalByteSent, int64_t totalBytesExpectedToSend) {
        NSLog(@"%lld, %lld, %lld", bytesSent, totalByteSent, totalBytesExpectedToSend);
    };
    
    put.objectMeta = @{
                       @"x-oss-meta-size":@"100"
                       };
    
    OSSTask * putTask = [client putObject:put];
    
    [putTask continueWithBlock:^id(OSSTask *task) {
    
        
        NSLog(@"objectKey: %@", put.objectKey);
        if (!task.error) {
            NSLog(@"upload object success!");
        } else {
            NSLog(@"upload object failed, error: %@" , task.error);
        }
        return nil;
    }];
    
    
    
}

- (NSString *)getDocumentDirectory {
    NSString * path = NSHomeDirectory();
    NSLog(@"NSHomeDirectory:%@",path);
    NSString * userName = NSUserName();
    NSString * rootPath = NSHomeDirectoryForUser(userName);
    NSLog(@"NSHomeDirectoryForUser:%@",rootPath);
    NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString * documentsDirectory = [paths objectAtIndex:0];
    return documentsDirectory;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
